using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.FileProviders;
using System.Reflection;

namespace RuoVea.OmiUI
{
    /// <summary>
    /// 服务集合扩展
    /// </summary>
    public static class ServiceCollectionExtensions
    {
        /// <summary>
        /// 添加RuoVea.OmiUI页面UI
        /// </summary>
        /// <param name="app"></param>
        /// <returns></returns>
        public static IApplicationBuilder UseOmiUIStaticAssets(this IApplicationBuilder app)
        {
            // 启用基本静态文件服务
            app.UseStaticFiles();
            
            // 获取当前程序集和程序集名称
            var assembly = typeof(ServiceCollectionExtensions).Assembly;
            var assemblyName = assembly.GetName().Name;
            
            // 创建嵌入文件提供程序，使用正确的基命名空间
            var fileProvider = new EmbeddedFileProvider(
                assembly,
                $"{assemblyName}.ruoveaui.xg6rvft8p"
            );
            
            // 配置静态文件服务，设置请求路径和缓存控制
            app.UseStaticFiles(new StaticFileOptions
            {
                FileProvider = fileProvider,
                RequestPath = "/ruoveaui/xg6rvft8p",
                ServeUnknownFileTypes = true,
                OnPrepareResponse = ctx =>
                {
                    ctx.Context.Response.Headers.Append("Cache-Control", "public,max-age=3600");
                }
            });

            return app;
        }
    }
}